<template>
	<view>
	
	<view class="userinfo">
		<view class="item">
			<view style="width: 40%;margin-top: -8px;">{{Text}}</view>
			<input type="text" value="123"  style="margin-top: 10px;width: 100%;" v-model="User.data"/>
			
		</view>
	</view>

	<view class="model">
		<view class='out'>
			<button type="default" @click="submit">提交</button>
		</view>
	</view>
	</view>
</template>

<script>
import { Icon } from 'element-ui';
	export default {
		data() {
			return {
				Text:"用户名称",
				User:{
					'data':'',
				},
				GetAllUserInfo:{
					
				},
				user_info:{
					"token":'',
				}
			}
		},
		onLoad:function() {
			let new_url;
			if(uni.getStorageSync('login-role')==1)
			new_url='http://192.168.2.226:8086/business/user/getinfo';
			else
			new_url='http://192.168.2.226:8086/business/seller/getinfo';
			this.get_user_info(new_url);		  
		},
		methods: {
			submit(){
				console.log(this.User.data);
				console.log(this.$route.query.type);
				if(this.$route.query.type==1)
				this.GetAllUserInfo.username=this.User.data;
				else if(this.$route.query.type==2)
				this.GetAllUserInfo.tele=this.User.data;
				else if(this.$route.query.type==3&&this.User.data=="男")
				this.GetAllUserInfo.gender=0;
				else if(this.$route.query.type==3&&this.User.data=="女")
				this.GetAllUserInfo.gender=1;
				if(this.$route.query.type==4)
				this.GetAllUserInfo.password=this.User.data;
				
				console.log(this.GetAllUserInfo.username);
				console.log(this.User.data)
				
				
				uni.request({
					url: 'http://192.168.2.226:8086/business/user/update',
					method:"POST",
					header: {
					        'token': this.user_info.token // 在这里设置请求头
					    },
					data:this.GetAllUserInfo,
					success() {
						uni.showToast({
							title:"修改成功",
							duration:2000,
							Icon:"success"
						})
					},
					fail() {
						
					}
				})
			},
			get_user_info(new_url){
				console.log("获取用户数据");
				this.user_info.token=uni.getStorageSync('login-user').token;
				console.log(this.user_info.token);
				uni.request({
				 	url:new_url,
				 	method:'GET',
					header: {
					        'token': this.user_info.token // 在这里设置请求头
					    },
					
				 	// 返回成功的结果
				 	success:(res) =>{
					console.log(res.data.data.username);
				 
					this.GetAllUserInfo = res.data.data;
					
					this.updateUserField(this.$route.query.type);
					console.log(this.GetAllUserInfo.username);
					},
				 	fail:(error) => {
				 		console.log("获取失败")
				 	},
				 })
				
			},
			
			updateUserField(type) {
			  switch (type) {
			    case '1':
			      this.Text = "用户名称";
			      this.User.data = this.GetAllUserInfo.username;
			      break;
			    case '2':
			      this.Text = "电话号码";
			      this.User.data = this.GetAllUserInfo.tele;
			      break;
			    case '3':
			      this.Text = "性别";
			      this.User.data = this.GetAllUserInfo.gender === 1 ? "女" : "男";
			      break;
				case '4':
				  this.Text = "密码";
				  this.User.data = this.GetAllUserInfo.password;
				  break;  
			    // 添加其他 case 处理
			    default:
			      // 默认情况或错误处理
			  }
			  console.log(this.User.data); // 现在这里应该有值了
			},


	},
	}
</script>

<style>
	.model{
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
.out{
	
	position: absolute;
    bottom: 2rem;	
	width: 50%;
}
.myinformation{
		display: flex;
		padding: 20px;
}
.myinformation .name{
	display: flex;
	/* 竖直排布 */
	flex-direction: column;
	/* 均匀分布 */
	justify-content: space-evenly;
}
.myinformation image{
	width: 100px;
	height: 100px;
	margin-right: 20px;
}
.username{
	font-size: 20px;
	font-weight: 900;
}
.realname .firstname{
	margin-right: 20px;
	
}
.realname .secondname{
	
}
.userinfo{
	background-color: rgba(0, 0, 0, 0.03);
	padding: 15px 0;
}
.userinfo .item{
	height: 50px;
	line-height: 60px;
	background-color: white;
	padding: 0 20px;
	display: flex;
	justify-content: space-between
}
</style>
